import { SearchService } from '../../../src/services/SearchEngine.mjs';

const { describe, test, expect, beforeEach } = await import('@jest/globals');

describe('SearchService', () => {
  let searchService;

  beforeEach(() => {
    searchService = new SearchService();
  });

  test('should be defined', () => {
    expect(searchService).toBeDefined();
  });

  test('should initialize correctly', async () => {
    await expect(searchService.init()).resolves.not.toThrow();
    expect(searchService.isInitialized()).toBe(true);
  });

  test('should perform search and return results', async () => {
    await searchService.init();
    const query = 'test query';
    const results = await searchService.search(query);
    
    expect(Array.isArray(results)).toBe(true);
    expect(results).toEqual(expect.arrayContaining([
      expect.objectContaining({
        id: expect.any(String),
        title: expect.any(String),
        content: expect.any(String),
        relevance: expect.any(Number)
      })
    ]));
  });

  test('should throw error if searching before initialization', async () => {
    await expect(searchService.search('test query'))
      .rejects
      .toThrow('SearchService not initialized');
  });

  test('should handle empty search query', async () => {
    await searchService.init();
    const results = await searchService.search('');
    expect(Array.isArray(results)).toBe(true);
    expect(results).toHaveLength(0);
  });

  test('should respect maxResults configuration', async () => {
    const maxResults = 5;
    searchService = new SearchService({ maxResults });
    await searchService.init();
    
    const results = await searchService.search('test');
    expect(results.length).toBeLessThanOrEqual(maxResults);
  });

  test('should properly clean up resources on destroy', async () => {
    await searchService.init();
    await expect(searchService.destroy()).resolves.not.toThrow();
    expect(searchService.isInitialized()).toBe(false);
  });
}); 